package com.px.base.mapper;

import com.px.common.core.mybatisplus.cache.MybatisPlusRedisCache;
import com.px.common.core.mybatisplus.core.BaseMapperPlus;
import com.px.base.domain.Region;
import com.px.base.vo.RegionDeptUserVo;
import com.px.base.vo.RegionSocialVo;
import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 行政地区Mapper接口
 *
 * @author 品讯科技
 * @date 2024-08
 */
// 如使需切换数据源 请勿使用缓存 会造成数据不一致现象
@CacheNamespace(implementation = MybatisPlusRedisCache.class, eviction = MybatisPlusRedisCache.class)
public interface RegionMapper extends BaseMapperPlus<Region> {

    @Select("select name from bs_region where deleted = 0 and id = #{id}")
    String queryName(@Param("id") Long id);

    @Select("select name from bs_region where deleted = 0 and adcode = #{adcode}")
    String queryNameByCode(@Param("adcode") String adcode);

    @Select("select id from bs_region where deleted = 0 and name = #{name} limit 1")
    Long queryIdByName(@Param("name") String name);

    @Select("select id from bs_region where deleted = 0 and adcode = #{adcode} limit 1")
    Long queryIdByCode(@Param("adcode") String adcode);

    /**
     * 通过州市名称查询code
     * @param name
     * @return
     */
    @Select("select adcode from bs_region where deleted = 0 and name = #{name} limit 1")
    String queryCodeByName(@Param("name") String name);

    @Select("select adcode from bs_region where deleted = 0 and id = #{id} limit 1")
    String queryCodeById(@Param("id") Long id);

    @Select("select * from bs_region where deleted = 0 and adcode = #{adcode} limit 1")
    Region queryByCode(@Param("adcode") String adcode);

    @Select("select parent_id from bs_region where deleted = 0 and id = #{id}")
    Long queryParentId(@Param("id") Long id);

    @Select("select name from bs_region where deleted = 0 and id = (select parent_id from bs_region where deleted = 0 and id = #{id} limit 1)")
    String queryParentName(@Param("id") Long id);

    @Select("select name from bs_region where deleted = 0 and id = (select parent_id from bs_region where deleted = 0 and adcode = #{adcode} limit 1)")
    String queryParentNameByCode(@Param("adcode") String adcode);

    @Select("select * from bs_region where deleted = 0 and id = (select parent_id from bs_region where deleted = 0 and id = #{id} limit 1)")
    Region queryParent(@Param("id") Long id);

    @Select("select id,name,level,adcode from bs_region where deleted = 0 and id = (SELECT region_id FROM sys_dept_region where deleted = 0 and dept_id = #{deptId} limit 1)")
    Region queryRegionByDept(@Param("deptId") Long deptId);


    // 查询区域对应社会经济人口列表
    List<RegionSocialVo> querySocialList(@Param("parentId") Long parentId, @Param("year") Integer year);

    // 查询区域对应社会经济人口
    RegionSocialVo querySocialListById(@Param("id") Long id, @Param("year") Integer year);

    // 查询行政区划_部门_用户列表
    List<RegionDeptUserVo> queryRegionDeptUserList();


    @Select("SELECT b.*  FROM sys_dept_region a LEFT JOIN bs_region b ON a.region_id = b.id  where a.dept_id=#{deptId}")
    Region getRegionByDeptId(@Param("deptId")Long deptId);
}
